<template>
    <div>
        <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto"
            class="demo-ruleForm" :size="formSize" status-icon>
            <el-form-item label="用户名" prop="username">
                <el-input v-model="ruleForm.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="ruleForm.password" />
            </el-form-item>
            <el-form-item prop="checked">
                <el-checkbox v-model="ruleForm.checked">
                    记住我
                </el-checkbox>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    登录
                </el-button>
            </el-form-item>
            <el-form-item label="验证码" prop="username">
                <el-input v-model="ruleForm.code" />
            </el-form-item>
        </el-form>



        <el-button type="primary" @click="toGitee">gitee第三方登录</el-button>
        <!-- <el-button type="success" @click="toRegister">注册</el-button> -->
        <el-button type="success" @click="$router.push('/register')">注册</el-button>
        <el-button type="success" @click="handleCode">获取验证码</el-button>
    </div>
</template>
<!-- 
账号一二十是管理员，账号三是普通用户
账号一：
username: admin
password: admin
账号二：
username: xielin
password: xielin
账号三：
username: user
password: user
-->
<script setup>
// import axios from 'axios'
import request from '@/untils/request';
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()

const ruleFormRef = ref()

const ruleForm = reactive({
    username: '',
    // 密码初始值123456
    password: '123456',
    checked: [],
    code: ''
})

const rules = reactive({
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 5, message: '输入内容在3到5个字之间', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 3, max: 6, message: '输入内容在3到6个字之间', trigger: 'blur' },
    ],
    checked: [
        {
            type: 'array',
            required: true,
            message: '请选择复选框',
            trigger: 'change',
        },
    ],
})

// const toRegister = () => {
//     router.push({
//         path: '/register'
//     })
// }

// 验证码
const handleCode = () => {
    const min = 100000
    const max = 999999
    const val = Math.floor(Math.random() * (max - min + 1)) + min
    console.log(val, '‘四位数验证码');
    ruleForm.code = val
}

const submitForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            // 请求登录接口，需要传参
            request.post('/login', ruleForm).then(res => {
                console.log(res);
                // 先判断，是否返回成功，如果成功就存储数据并且跳转页面，否则就提示接口返回的message信息
                // 接口10000表示成功
                if (res.data.code === 10000 && res.status === 200) {
                    ElMessage({
                        message: '登录成功',
                        type: 'success',
                    })
                    // 把接口返回的用户名和token存储本地
                    localStorage.setItem('username', res.data.data.userInfo.username)
                    localStorage.setItem('accessToken', res.data.data.accessToken)
                    localStorage.setItem('refreshToken', res.data.data.refreshToken)
                    // 头像
                    localStorage.setItem('avator', res.data.data.userInfo.avator)
                    // 跳转首页
                    router.push({
                        path: '/'
                    })
                } else {
                    ElMessage.error(res.data.message)
                }
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

// 第三方登录
const toGitee = () => {
    window.open('https://gitee.com/oauth/authorize?client_id=d4818f856905e5eeeaec0f9a640d6942af36656e88cc4c67eeff776ed8fe5127&redirect_uri=http://localhost:5173/oauth/xielin&response_type=code')
}
</script>
